<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/9/21
 * Time: 15:08
 */

namespace app\admin\validator;

use app\service\CaptchaService;
use lib\common\CommonValidator;

class LoginValidator extends CommonValidator
{
    protected $rule = [
        'username' => 'require|length:5,20',
        'password' => 'require|length:6,20',
//        'token'    => 'require|length:32',
//        'code'     => 'require|validCaptcha'
    ];

    protected $message = [
        'username.require'  => '请输入账号或手机号',
        'username.length'   => '账号长度5-20字符',
        'password.require'  => '请输入密码',
        'password.length'   => '密码长度6,20字符',
        'token'             => '验证码数据异常',
        'code.require'      => '请输入验证码',
        'code.validCaptcha' => '验证码不正确'
    ];

    protected function validCaptcha ($value, $rule, array $data = []): bool {
        $service = make(CaptchaService::class);
        return $service->verify($data['code'], $data['token']);
    }
}
